﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.SqlClient;

namespace Profari_Heldur_Utan_Um_Prof
{
    public partial class ListOfProjects : Form
    {

        #region SQL Connections
        private static SqlConnection con = new SqlConnection("Data Source=orninn.com;Initial Catalog=Notendaprofanir;Persist Security Info=True;User ID=SonjaNew;Password=Léttjógúrt ");
        private static DataTable dt = new DataTable();
        //private static SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Project", con);
        private static DataSet ds = new DataSet();
        SqlDataReader r = null;
        #endregion

        #region Variables
        public static int globalGroupID;
        public static int globalEvaluationID;
        int countHelpPressed = 1;
        #endregion

        public ListOfProjects()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
        }        
        
        private void ListOfProjects_Load(object sender, EventArgs e)
        {
            lblHelpText1.Hide();
            getGroupName();
            if(groupListBox.Items.Count > 0)
                groupListBox.SelectedIndex = 0;
            lblHelpText2.Hide();
        }

        //The code for the listboxes if value in them are selected
        #region Listboxes
        private void groupListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            globalGroupID = getGroupID();
            evaluationListBox.Items.Clear();
            getEvaluationName();
            if (evaluationListBox.Items.Count > 0)
                evaluationListBox.SelectedIndex = 0;
            globalEvaluationID = getEvaluateID();
           
        }

        private void evaluationListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            globalEvaluationID = getEvaluateID();
        }
        #endregion

        #region Buttons
        private void Back_Click(object sender, EventArgs e)
        {
            System.Threading.Thread testerInterface = new System.Threading.Thread(new System.Threading.ThreadStart(OpenTesterInterface));
            testerInterface.SetApartmentState(System.Threading.ApartmentState.STA);
            testerInterface.Start();
            this.Close();

        }
        
        private void Next_Click(object sender, EventArgs e)
        {
            if ((evaluationListBox.SelectedIndex) >= 0)
            {
                System.Threading.Thread sessionView = new System.Threading.Thread(new System.Threading.ThreadStart(OpenSessionView));
                sessionView.SetApartmentState(System.Threading.ApartmentState.STA);
                sessionView.Start();
                this.Close();
            }
            else
            {
                MessageBox.Show("You must pick a session to view data for");
            }
        }

        private void Edit_Click(object sender, EventArgs e)
        {
            if (groupListBox.SelectedIndex >= 0 && evaluationListBox.SelectedIndex >= 0)
            {
                System.Threading.Thread editTask = new System.Threading.Thread(new System.Threading.ThreadStart(OpenEditTask));
                editTask.SetApartmentState(System.Threading.ApartmentState.STA);
                editTask.Start();
                this.Close();
                /*
                EditTask editTask1 = new EditTask(globalGroupID,globalEvaluationID);
                editTask1.Show();
                this.Close();*/
            }
            else
            {
                MessageBox.Show("You must select a project and a session to edit");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult cancelProject = MessageBox.Show("Are you sure you want to stop viewing old projects?", "Quit", MessageBoxButtons.YesNo);
            if (cancelProject == DialogResult.Yes)
            {
                System.Threading.Thread testerInterface = new System.Threading.Thread(new System.Threading.ThreadStart(OpenTesterInterface));
                testerInterface.SetApartmentState(System.Threading.ApartmentState.STA);
                testerInterface.Start();
                this.Close();
            }
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            if (countHelpPressed % 2 == 1)
            {
                lblHelpText1.Show();
                countHelpPressed++;
            }
            else if (countHelpPressed % 2 == 0)
            {
                lblHelpText1.Hide();
                countHelpPressed++;
            }
        }

        private void btnHelp2_Click(object sender, EventArgs e)
        {
            if (countHelpPressed % 2 == 0)
            {
                lblHelpText2.Hide();
                countHelpPressed++;
            }
            else if (countHelpPressed % 2 == 1)
            {
                lblHelpText2.Show();
                countHelpPressed++;
            }
        }
        #endregion

        #region SQL Commands
        private void getGroupName()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT name FROM Groups", con);
                r = cmd.ExecuteReader();
                while (r.Read())
                {
                    groupListBox.Items.Add(r["Name"]);
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());
            }
            finally
            {
                if (con != null)
                    con.Close();
            }
            Hashtable hash = new Hashtable();
            int cnt = 0;
            foreach (string item in groupListBox.Items)
            {
                if (!hash.ContainsValue(item))
                {
                    hash[cnt++] = item;
                }
            }

            groupListBox.Items.Clear();
            for (int i = 0; i < cnt; i++)
            {
                groupListBox.Items.Add(hash[i]);
            }


        }

        private int getGroupID()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT id FROM Groups WHERE name ='" + (string)groupListBox.SelectedItem + "';", con);
                r = cmd.ExecuteReader();
                while (r.Read())
                {
                    int thisrow = r.GetInt32(0);
                    return thisrow;
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());
            }
            finally
            {
                if (con != null)
                    con.Close();
            }
            return 0;
        }

        private void getEvaluationName()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("SELECT E.name FROM Evaluation E INNER JOIN GroupEvaluations GE ON GE.evaluationID = E.id WHERE GE.groupsID=" + globalGroupID, con);
                r = cmd.ExecuteReader();
                while (r.Read())
                {
                    evaluationListBox.Items.Add(r["Name"]);
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());
            }
            finally
            {
                if (con != null)
                    con.Close();
            }
        }

        private int getEvaluateID()
        {
            try
            {
                con.Open();
                int evaluationID = 0;
                //SqlCommand cmd = new SqlCommand("SELECT id FROM Evaluation WHERE name ='" + (string)evaluationListBox.SelectedItem + "';", con);
                SqlCommand cmd = new SqlCommand("SELECT E.id FROM Evaluation E INNER JOIN GroupEvaluations GE ON GE.evaluationID = E.id WHERE GE.groupsID='" + globalGroupID + "' AND E.name ='" + (string)evaluationListBox.SelectedItem + "';", con);
                 
                r = cmd.ExecuteReader();

                while (r.Read())
                {
                    return evaluationID = r.GetInt32(0);
                }
            }
            catch (SqlException ex)
            {
                Console.WriteLine("Error: {0}", ex.ToString());
            }
            finally
            {
                if (con != null)
                    con.Close();
            }
            return 0;
        }
        #endregion

        //The Open forms functions makes it able to move between forms and close the first form when the second one is opened
        #region OpenForm
        public static void OpenTesterInterface()
        {
            Application.Run(new TesterInterface());
        }


        public static void OpenEditTask()
        {
            Application.Run(new EditTask());
        }

        public static void OpenSessionView()
        {
            Application.Run(new SessionView());
        }
        #endregion

    }
}
